package com.sun.entity;

import com.sun.enums.HttpEnum;

/**
 * @author SunRui
 * @version 1.0.0
 * @ClassName R.java
 * @Description 统一结果封装类
 */
public class R<T> {
    //描述统一格式的数据
    private T data;
    //描述统一格式中的编码，用于区分操作，可以简化配置0或1表示成功失败
    private Integer code;
    //描述统一格式中的消息，可选属性
    private String message;
    //信息状态
    private Boolean status;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    private static <T>R<T> response(Integer code,Boolean status,String message,T data){
        R<T> R = new R<>();
        R.setCode(code);
        R.setStatus(status);
        R.setMessage(message);
        R.setData(data);
        return R;
    }

    private static <T>R<T> response(Integer code,Boolean status,String message){
        R<T> R = new R<>();
        R.setCode(code);
        R.setStatus(status);
        R.setMessage(message);
        return R;
    }

    //成功返回，无返回结果
    public static <T>R<T> success(){
        return response(HttpEnum.SUCCESS.getCode(), true, HttpEnum.SUCCESS.getMessage());
    }
    //成功返回，有返回结果
    public static <T>R<T> success(T data){
        return response(HttpEnum.SUCCESS.getCode(), true, HttpEnum.SUCCESS.getMessage(),data);
    }
    //失败返回
    public static <T> R<T> fail() {
        return response(HttpEnum.ERROR.getCode(), false, HttpEnum.ERROR.getMessage());
    }
    //失败返回
    public static <T> R<T> fail(Integer code,String message) {
        return response(code, false, message);
    }


}

